import Vue from 'vue'
import VueRouter from "vue-router"

import Index from '@/views/Index'
import login from '@/views/Login'
import NotFound from "@/views/NotFound"
import store from '@/store/index'


Vue.use(VueRouter)

let routes = [
  {
    path: '/',
    name: 'index',
    component: Index,
    meta: { requiresAuth: true },
    redirect: '/file',
    children: [
      {
        path: '/file',
        name: '文件',
        icon: 'my-cloud_disk-file',
        component:()=>import('@/views/public/file'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/photo',
        name: '相册',
        icon: 'my-cloud_disk-photo',
        component:()=>import('@/views/public/photo'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/sort',
        name: '分类',
        icon: 'my-cloud_disk-sort',
        component:()=>import('@/views/public/sort'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/collect',
        name: '收藏夹',
        icon: 'my-cloud_disk-collect',
        component:()=>import('@/views/public/collect'),
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/recycle',
        name: '回收站',
        icon: 'my-cloud_disk-recycle',
        component:()=>import('@/views/public/recycle'),
        meta: {
          requireAuth: true
        }
      }
    ],
    beforeEnter: function (to, from, next) {  //独享路由守卫
      try {
        document.getElementById('paper').className = 'back_out';
        setTimeout(() => {
          next()
        }, 1500)
      } catch (e) {
        next()
      }
    }
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  // 将匹配所有内容并将其放在 `$route.params.pathMatch` 下，404
  {path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound},

]
const router = new VueRouter({
  mode: "history",
  routes
})
// 全局路由守卫,前端登录拦截
router.beforeEach((to, from, next) => {
  if (to.path === '/login')return next();
  store.state.token?next():next({path:"/login"});
})
export {router}
